package test.org.nanotek.datanucleus.persistence.name;

import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.nanotek.base.NamePerson;
import org.nanotek.base.NamePersonType;
import org.nanotek.base.StringBase;
import org.nanotek.base.artist.Artist;
import org.nanotek.base.calendar.date.BirthDate;
import org.nanotek.base.person.Gender;
import org.nanotek.persistence.jpa.brainz.artist.ArtistDAO;

public class ArtistDataNucleusMappedEntityTest {

	EntityManagerFactory emf;
	EntityManager em;
	ArtistDAO dao;
	
	@Before
	public void start()
	{ 
		emf = Persistence.createEntityManagerFactory("musicbrainz");
		em = emf.createEntityManager();
		dao = new ArtistDAO();
		dao.setEntityManager(em);
	}
	
	@Test
	public void test() {
		Artist artist = new Artist(); 
		artist.setGender(Gender.Male);
		NamePerson stringFirst = new NamePerson(NamePersonType.GivenName, 0,new StringBase("first1"));//new NameString(0,new StringBase("first1"));
		NamePerson stringLast = new NamePerson(NamePersonType.LastName,1,new StringBase("last1"));
//		Set<StringPositionBase> authorNames = new HashSet<StringPositionBase>();
		artist.getName().add(stringFirst);
		artist.getName().add(stringLast);
		BirthDate birthDate = new BirthDate(new Date());
		birthDate.getPersonsBirthDate().add(artist);
		artist.setDateBirth(birthDate);
		EntityTransaction transaction = em.getTransaction();
		transaction.begin(); 
		em.persist(artist);
		transaction.commit();
	}

	@After
	public void shutDown()
	{ 
		if (em !=null && em.isOpen()) 
			em.close();
		if (emf !=null && emf.isOpen()) 
			emf.close();
	}
	
}
